package bookmanagementsystem.gui;

import bookmanagementsystem.model.BorrowingRecord;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class BorrowingRecordsFrame extends JFrame {
    private JList<String> borrowingList;

    public BorrowingRecordsFrame(List<BorrowingRecord> borrowingRecords) {
        setTitle("借书记录");
        setSize(500, 300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        initComponents(borrowingRecords);
        addComponentsToFrame();
    }

    private void initComponents(List<BorrowingRecord> borrowingRecords) {
        List<String> recordInfoList = new ArrayList<>();
        if (borrowingRecords != null && !borrowingRecords.isEmpty()) {
            for (BorrowingRecord record : borrowingRecords) {
                String recordInfo = "借书记录ID：" + record.getRecordID() + "\n" +
                        "图书ID：" + record.getBookID() + "\n" +
                        "借阅时间：" + record.getBorrowingTime() + "\n\n";
                recordInfoList.add(recordInfo);
            }
        }
        borrowingList = new JList<>(recordInfoList.toArray(new String[0])); // 直接使用数组创建JList
    }

    private void addComponentsToFrame() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(new JLabel("您的借书记录如下："), BorderLayout.NORTH);
        panel.add(new JScrollPane(borrowingList), BorderLayout.CENTER);
        getContentPane().add(panel);
    }
}